package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("validateCode")
public class ValidateCodeController {
    @RequestMapping("getCode")
    public void getCode(HttpServletRequest request,HttpServletResponse response)throws Exception {
        //从工具类中随机自动生成验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        System.out.println("validateCode="+captcha.getCode());
        //获取session将随机生成的验证码放到session中
        HttpSession session = request.getSession();
        session.setAttribute("code", captcha.getCode());
        String code = (String)session.getAttribute("code");
        System.out.println(code);
        //然后将图片响应出去
        captcha.write(response.getOutputStream());
    }
}
